package com.example.administrator.u17.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.InfoBaseAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.modle.Info;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class ReadActivity extends AppCompatActivity {

    private ListView lv;
    private BaseAdapter mAdapter;
    private List<Info.Data.ReturnData.Imagelist> mData;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_read);
        initView();
        initData();
        setData();
    }

    private void setData() {
        loadData();
        lv.setAdapter(mAdapter);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new InfoBaseAdapter(this, mData);
        dialog = new ProgressDialog(this);
        dialog.setTitle("加载中");
        dialog.show();
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv_activity_read);
    }

    private void loadData() {
        String id = getIntent().getStringExtra("id");
        x.http().get(new RequestParams(NetConfig.INFO_PATH + id), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Info info = new Gson().fromJson(result, Info.class);
                for (Info.Data.ReturnData.Imagelist imagelist : info.data.returnData.image_list) {
                    mData.add(imagelist);
                }
                mAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
